package bsf.baseservice.businessmq.producter;

import bsf.baseservice.businessmq.systemruntime.BusinessMQException;

/**
 线程安全上下文集合
 
*/
public class SynchronousContextList extends java.util.ArrayList<ProducterContext>
{
	private Object lock = new Object();

	public  void addContext(ProducterContext context)
	{
		synchronized (lock)
		{
			for (ProducterContext o : this)
			{
				if (context.ProducterProvider.getMQPath().equals(o.ProducterProvider.getMQPath()))
				{
					throw new BusinessMQException(String.format("队列%1$s的上下文已注册", context.ProducterProvider.getMQPath()));
				}
			}
			super.add(context);
		}
	}

	public void removeContext(ProducterContext context)
	{
		synchronized (lock)
		{
			ProducterContext t = null;
			for (ProducterContext o : this)
			{
				if (context.ProducterProvider.getMQPath().equals(o.ProducterProvider.getMQPath()))
				{
					t = o;
				}
			}
			if(t!=null)
			{
				super.remove(t);
			}
		}
	}
	/** 
	 拷贝快照
	 
	 @return 
	*/
	public  java.util.ArrayList<ProducterContext> copyToList()
	{
		synchronized (lock)
		{
			java.util.ArrayList<ProducterContext> t = new java.util.ArrayList<ProducterContext>();
			for (ProducterContext o : this)
			{
				t.add(o);
			}
			return t;
		}
	}
}